package event;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Random;

public class ReferenceAttrGenerator extends AttrGenerator{
	private BasicConfig basicConfig = null;
	private int totalSplits = 1;
	private int splitId = 0;
	public ReferenceAttrGenerator(int totalSplits, int splitId)
	{
		basicConfig = new BasicConfig();
		this.totalSplits = totalSplits;
		this.splitId = splitId;
	}
	@Override
	// generate the reference page id from a range of all the pages
	public String generate(List<String> params) {
		String name = params.get(0);
		Basic basic = basicConfig.getBasic(name);
		long total = basic.getNumber();
		long start, range;
		if(totalSplits == 1)
		{
			start = 0;
			range = total;
		}
		else
		{
			long numPerRegion =  total / totalSplits;
			start = splitId * numPerRegion;
			if(splitId < totalSplits - 1)
			{
				range = numPerRegion;
			}
			else
			{
				range = total - start;
			}
		}
		
		Random random = new Random();
		if(start + range < Integer.MAX_VALUE)
		{
			int high = (int) range;
			int number = (int)start + random.nextInt(high);
			DecimalFormat formatter = new java.text.DecimalFormat("000000000000");
			String id = formatter.format(number);
			return name + "-" + id;
		}
		else
		{
			long number = start + Math.abs(random.nextLong()) % range;
			DecimalFormat formatter = new java.text.DecimalFormat("000000000000");
			String id = formatter.format(number);
			return name + "-" + id;
		}
	}
}
